package ControlLayer;
import java.util.ArrayList;
import ModelLayer.*;
import java.util.*;


public class LoanCtr
{
    private AddressCtr addrCtr;
    private DVDCtr dvdCtr;
    private CollectionOfLoan loanCollection;
    
    /* Constructor for class LoanCtr */
    public LoanCtr(AddressCtr addrCtr, DVDCtr dvdCtr)
    {
        loanCollection = new CollectionOfLoan();
        this.addrCtr = addrCtr;
        this.dvdCtr = dvdCtr;
    }
     
    /* Method to serch and print information for a loan */
    public void getLoan(long id)
    {
         boolean find = true;
            for(Loan object : loanCollection.loans) {
                if(object.getId() == id) {
                    find = false;
                    System.out.println("ID : " + object.getId());
                    System.out.println("BorrowDate : " + object.getBorrowDate());
                    System.out.println("Period : " + object.getPeriod());
                   
                }   
            }
            
            if(find) {
                System.out.println("No match");
            }
    }
    
    /* Method to create a loan */
    public void createLoan(Copy copy, long id, long borrowerId, int borrowDate, String period)
    {
         Loan object = new Loan(copy,id,borrowDate,period);
            if(loanCollection.loans.contains(object)){
                System.out.println("The Loan is already created");
            }
            else{
                loanCollection.loans.add(object);
                addrCtr.findPerson(borrowerId).addLoan(object);
            }
                     
    }
    
    /* Method to delet a specifc loan */
     public void deleteLoan(long id)
    {
        Iterator<Loan> it = loanCollection.loans.iterator();
            while(it.hasNext()) {
                Loan d = it.next();
                if(d.getId() == id) {
                    it.remove();
                }
                else {
                    System.out.println("ID invalid");
                }
            }    
    }
    
    /* Method to print all loans */
    public void listAllLoans()
    {
        for(Loan loan : loanCollection.loans){
            loan.printLoanInfo();
        }
    }
    
    /* Method to check if a person is in the system */
    public boolean isPerson(long id)
    {
        if(addrCtr.findPerson(id) == null)
        {
            return false;
        }
        return true;
    }
    
    /* ??? */
    public Copy getCopy(long dvdId, long copyId)
    {
        return dvdCtr.getCopy(dvdId, copyId);
    }
    
}


